package com.wuyi.tea.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyi.tea.domain.dto.ArticleDTO;
import com.wuyi.tea.domain.pojo.Article;
import com.wuyi.tea.domain.pojo.Comment;
import com.wuyi.tea.domain.pojo.Picture;
import com.wuyi.tea.domain.pojo.User;
import com.wuyi.tea.domain.vo.ArticleVO;
import com.wuyi.tea.domain.vo.CommentVO;
import com.wuyi.tea.domain.vo.UserVO;
import com.wuyi.tea.enums.ConstantEnum;
import com.wuyi.tea.mapper.ArticleMapper;
import com.wuyi.tea.mapper.CommentMapper;
import com.wuyi.tea.mapper.PictureMapper;
import com.wuyi.tea.mapper.UserMapper;
import com.wuyi.tea.service.ArticleService;
import com.wuyi.tea.service.PictureService;
import com.wuyi.tea.util.LocalStorageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 30793
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public void releaseByarticle(ArticleDTO articleDTO) {
        //文章处理
        Article article= BeanUtil.copyProperties(articleDTO,Article.class);
        article.setLikeNumber(0);
        article.setUserId(Long.valueOf(LocalStorageUtil.get().toString()));
        articleMapper.insert(article);

        //图片处理
        List<String> picturedtoList = articleDTO.getPictures();
        if (!CollectionUtils.isEmpty(picturedtoList)){
            for (String picture : picturedtoList) {
                Picture newPicture= Picture.builder()
                        .picture(picture)
                        .orient(ConstantEnum.ORIENT_ARTICLE)
                        .orientId(article.getId())
                        .build();
                pictureMapper.insert(newPicture);
            }
        }

    }

    @Override
    public void addLikeArticle(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setLikeNumber(article.getLikeNumber() + 1);
        articleMapper.updateById(article);
    }

    @Override
    public List<ArticleVO> getArticleVoList() {
        List<Article> articles = articleMapper.selectList(null);
        List<ArticleVO> articleVOList=new ArrayList<>();

        for (Article article : articles) {
            Long userId = article.getUserId();

            User user = userMapper.selectById(userId);
            //照片
            List<Picture> pictureList = pictureMapper.selectList(
                    new LambdaQueryWrapper<Picture>()
                        .eq(Picture::getOrientId, article.getId())
                        .eq(Picture::getOrient,ConstantEnum.ORIENT_ARTICLE)
            );
            //评论
            List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                    .eq(Comment::getOrientId,article.getId())
                    .eq(Comment::getOrient,ConstantEnum.ORIENT_ARTICLE)
            );

            List<CommentVO> commentVOList=new ArrayList<>();
            for (Comment comment : comments) {
                CommentVO commentVO=new CommentVO();
                //用户
                User tUser = userMapper.selectById(userId);
                UserVO userVO= BeanUtil.copyProperties(tUser,UserVO.class);
                commentVO.setUser(userVO);

                BeanUtils.copyProperties(comment,commentVO);
                commentVOList.add(commentVO);
            }
            //拷贝
            ArticleVO articleVO=BeanUtil.copyProperties(article,ArticleVO.class);
            //将用户信息封装进来
            UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
            articleVO.setUser(userVO);
            if(!CollectionUtils.isEmpty(pictureList)){
                articleVO.setPictures(pictureList);
            }
            articleVO.setComments(commentVOList);
            articleVOList.add(articleVO);
        }

        return articleVOList;
    }

    @Transactional
    @Override
    public void deleteArticle(Long id) {
        //  删除文章的介绍图片
        pictureMapper.delete(new LambdaQueryWrapper<Picture>()
                .eq(Picture::getOrientId, id)
                .eq(Picture::getOrient, ConstantEnum.ORIENT_ARTICLE)
        );
        //  删除文件的相关评论
        commentMapper.delete(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getOrientId, id)
                .eq(Comment::getContent, ConstantEnum.ORIENT_COMMENT)
        );
        //  删除文章
        articleMapper.deleteById(id);
    }
}
